package com.chen.authserver.v1.controller;

import com.chen.authserver.constant.RespCode;
import com.chen.authserver.resp.RespMode;
import com.chen.authserver.v1.VO.AuthUserVO;
import com.chen.authserver.v1.service.TokenService;
import com.chen.authserver.v1.service.UserService;
import com.chen.authserver.v1.service.UserVerifyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api("v1鉴权接口")
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/v1")
public class AuthV1Controller {

    @Autowired
    UserVerifyService userVerifyService;

    @Autowired
    TokenService tokenService;

    @Autowired
    UserService userService;

    static RespMode noTokenResp = null;

    static RespMode illegaltoken = null;

    static {
        RespMode.Builder<String> builder = new RespMode.Builder<>();
        builder.code(RespCode.REQUEST_WITHOUT_TOKEN_CODE);
        builder.msg(RespCode.REQUEST_WITHOUT_TOKEN_MSG);
        noTokenResp = builder.build();

        builder.code(RespCode.TOKEN_VALIDATE_FAIL_CODE);
        builder.msg(RespCode.TOKEN_VALIDATE_FAIL_MSG);
        illegaltoken = builder.build();
    }

    @ApiOperation("测试接口")
    @GetMapping("/test")
    public String test() {
        return "success";
    }

    @ApiOperation("查询用户")
    @GetMapping("/find")
    public RespMode<AuthUserVO> find(@RequestParam(name = "q") String query) {
        RespMode.Builder<AuthUserVO> builder = new RespMode.Builder<>();
        AuthUserVO result = userService.findUserByQuery(query);
        builder.success(result);
        return builder.build();
    }

    @ApiOperation("获取登陆加密公钥接口")
    @GetMapping("/secretkey")
    public RespMode<String> publickey() {
        RespMode.Builder<String> builder = new RespMode.Builder<>();
        builder.success(userVerifyService.getPublicKey());
        return builder.build();
    }

    @RequestMapping("/notoken")
    public RespMode<String> noToken() {
        return noTokenResp;
    }

    @RequestMapping("/illegaltoken")
    public RespMode<String> illegalToken() {
        return illegaltoken;
    }
}
